﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Plataforma2D_Kaz
{
    class Players : Sprite
    {
        Animation attack;
        Animation jump;
        Animation damage;
        Animation walking;
        Animation idle;
        Animation fall;
        public int player;
        private int jumpStr;



        public Players(Texture2D image, GameWindow Window)
            : base(image, Window)
        {

            controlAnimations();
            position = new Vector2(350, 50);
            speed = new Vector2(5, 5);
            this.animation = idle;
            colision = new Rectangle((int)position.X, (int)position.Y, idle.frame_X, idle.frame_Y);
            this.mass = 1;
            this.player = 1;
            this.jumpStr = 30;


        }
        private void controlAnimations()
        {
            attack = new Animation();
            jump = new Animation();
            walking = new Animation();
            damage = new Animation();
            idle = new Animation();
            fall = new Animation();
            //Attack//
            //Jump//
            //Walking//
            //Damage//
            //Idle//
            idle.framesTotal = 4;
            idle.fps = 3;
            idle.Y = 0;
            idle.frame_X = 43;
            idle.frame_Y = 50;

            //fall//
            fall.framesTotal = 2;
            fall.fps = 3;
            fall.Y = 570;
            fall.frame_X = 46;
            fall.frame_Y = 57;


        }
        public void controlMove(KeyboardState currentKey, KeyboardState lastKey, MouseState currentMouse, MouseState lastMouse,ref int bgSourceX, ref float ptPosX, ref int bg2SourceX)
        {
            if (player == 1)
            {
                rightDirection = true;
                if (currentKey.IsKeyDown(Keys.Right))
                {
                    position.X += speed.X;
                    this.isMoving = true;
                }

                if (currentKey.IsKeyDown(Keys.Left))
                {
                    position.X -= speed.X;
                    this.isMoving = true;
                }

                if (currentKey.IsKeyDown(Keys.Down))
                {
                    position.Y += speed.Y;
                    this.isMoving = true;
                }

                if (currentKey.IsKeyDown(Keys.Up))//&& lastKey.IsKeyUp(Keys.Up))
                {
                    jumpStr = 5;
                    position.Y -= speed.Y * jumpStr;
                    this.isFall = true;
                }


            }
            
            #region blockMovePlayer

            if (position.X >= Window.ClientBounds.Width - colision.Width)
                position.X = Window.ClientBounds.Width - colision.Width;

            if (position.Y >= Window.ClientBounds.Height - colision.Height)
                position.Y = Window.ClientBounds.Height - colision.Height;

            if (position.X <= 0)
                position.X = 0;

            if (position.Y <= 0)
                position.Y = 0;
            #endregion

            if (this.position.X > (Window.ClientBounds.Width) * 2 / 3)
            {
                bgSourceX -= (int)(speed.X);
                bg2SourceX -= (int)(speed.X / 3);
                ptPosX -= (int)speed.X;
                this.position.X -= (int)(speed.X); 
             }

            if (((this.position.X < Window.ClientBounds.Width/3)&&(this.position.X > 0)))
            {
                bgSourceX += (int)speed.X;
                bg2SourceX += (int)(speed.X / 3);
                ptPosX += (int)speed.X;
                this.position.X += (int)speed.X;
            }

            if ((currentMouse.LeftButton == ButtonState.Pressed))
            {
                position = new Vector2(currentMouse.X, currentMouse.Y);
                if (lastMouse.LeftButton != ButtonState.Pressed)
                {
                    //efeitoSonoro.Play();
                }
            }
        }
        public override void Update(GameTime gameTime)
        { }
        public void Update(GameTime gameTime, KeyboardState currentKey, KeyboardState lastKey, MouseState currentMouse, MouseState lastMouse,ref int bgSourceX, ref float ptPosX, ref int bg2SourceX)
        {
            this.gravity(fall);
            currentAnim = animation;
            colision.X = (int)position.X;
            colision.Y = (int)position.Y;
            controlMove(currentKey, lastKey, currentMouse, lastMouse,ref bgSourceX, ref ptPosX, ref bg2SourceX);
            if (this.gravity(fall) == false)
            {
                this.animation = idle;
            }
            if (jumpStr > 0)
            {
                jumpStr -= 1;
            }

        }
    }
}
